package xyz.xiezc.www.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import xyz.xiezc.www.entity.MziTag;
import xyz.xiezc.www.entity.MzituBean;
import xyz.xiezc.www.entity.MzituImgBean;
import xyz.xiezc.www.mapper.MziTagMapper;
import xyz.xiezc.www.mapper.MzituBeanMapper;
import xyz.xiezc.www.mapper.MzituImgBeanMapper;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
@Controller
@RequestMapping("/")
public class DealController {
    @Autowired
    MzituBeanMapper mzituBeanMapper;
    @Autowired
    MzituImgBeanMapper mzituImgBeanMapper;

    @Autowired
    MziTagMapper mziTagMapper;

    @GetMapping("updateTag")
    @ResponseBody
    public String updateTag() {
        ExecutorService executorService = Executors.newFixedThreadPool(2);
        LambdaQueryWrapper<MzituImgBean> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByAsc(MzituImgBean::getId);
        lambdaQueryWrapper.ge(MzituImgBean::getId, 24462);
        Page page = new Page();
        page.setSize(1000);
        int pageNo = 1;
        while (true) {
            page.setCurrent(pageNo);
            IPage<MzituImgBean> iPage = mzituImgBeanMapper.selectPage(page, lambdaQueryWrapper);
            log.info("pageNo: {} , pageSize:{} ,  totalPage:{}", pageNo, 1000, iPage.getPages());
            executorService.execute(() -> {
                for (MzituImgBean mzituImgBean : iPage.getRecords()) {
                    MziTag mziTag = new MziTag();
                    mziTag.setPath(mzituImgBean.getPath());
                    LambdaQueryWrapper<MziTag> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
                    lambdaQueryWrapper1.eq(MziTag::getPhotoId, mzituImgBean.getId());
                    mziTagMapper.update(mziTag, lambdaQueryWrapper1);
                    try {
                        log.info("path:{} , img_id:{}",mziTag.getPath(),mzituImgBean.getId());
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                log.info("处理完毕: pageNo: {} ,  totalPage:{}", iPage.getCurrent(), iPage.getPages());
            });
            if (pageNo == iPage.getPages()) {
                break;
            }
            pageNo++;
        }
        return "结束";
    }


    @GetMapping("deal")
    @ResponseBody
    public String dealDb() {
        LambdaQueryWrapper<MzituBean> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.isNull(MzituBean::getFirstImg);
        List<MzituBean> mzituBeans = mzituBeanMapper.selectList(queryWrapper);
        for (MzituBean mzituBean : mzituBeans) {
            LambdaQueryWrapper<MzituImgBean> mzituImgBeanLambdaQueryWrapper = new LambdaQueryWrapper<>();
            mzituImgBeanLambdaQueryWrapper.eq(MzituImgBean::getUniqueStr, mzituBean.getUniqueStr());
            mzituImgBeanLambdaQueryWrapper.orderByAsc(MzituImgBean::getCreateTime);
            List<MzituImgBean> mzituImgBeans = mzituImgBeanMapper.selectList(mzituImgBeanLambdaQueryWrapper);
            if (mzituImgBeans.isEmpty()) {
                log.info("没有图片,{}", JSON.toJSONString(mzituBean));
                continue;
            }
            MzituImgBean mzituImgBean = mzituImgBeans.get(0);
            String path = mzituImgBean.getPath();
            mzituBean.setFirstImg(path);
            if (mzituBean.getRealTime() == null) {
                mzituBean.setRealTime(mzituImgBean.getShowTime());
            }
            if (mzituBean.getType() == null) {
                mzituBean.setType(mzituBean.getType());
            }

            mzituBeanMapper.updateById(mzituBean);
        }
        return mzituBeans.size() + "";
    }
}
